<?php
    function check_dir($pid, $OJ_DATA){
        $path = $OJ_DATA . '/' . $pid;
        $ret = false;
        $fp = fopen($path."/testifwriteable.tst","w");
        $ret = !($fp === false);
        fclose($fp);
        unlink($path."/testifwriteable.tst");
        return $ret;
   	}

	function file_get($pid, $OJ_DATA, $filename){
		$path = $OJ_DATA . '/' . $pid . '/' . $filename;
		$result = array();
		$filein = '';$fileout = '';
		if (file_exists($path . '.in')){
			$filein = file_get_contents($path . '.in');
		}
		if (file_exists($path . '.out')){
			$fileout = file_get_contents($path . '.out');
		}
		array_push($result, $filein);
		array_push($result, $fileout);
		return $result;
	}
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<link rel=stylesheet href='../include/hoj.css' type='text/css'>
<title>Edit Problem Data</title>
</head>
<body>
<center>
<?php require_once("../include/db_info.inc.php");
$id = (int)$_GET['id'];
$filename = $_GET['file'];


if (strpos($filename, '/') || strpos($filename, '\\')){
	echo "File Name Error!\n";
	exit(1);
}

if (!(isset($_SESSION['administrator']))){
	echo "<a href='../loginpage.php'>Please Login First!</a>";
	exit(1);
}

if (!check_dir($id, $OJ_DATA)){
	echo "Problem Error!";
	exit(1);
}
?>
<p align="center"><font color="#333399" size="4">Welcome To Administrator's Page of Judge Online of ACM ICPC, <?php echo $OJ_NAME?>.</font></p>

<?php
$data = file_get($id, $OJ_DATA, $filename);
?>
<form action="problem_data_edit.php" method="post">
<table width="100%" border="1">
<tr><td>Input</td><td>Output</td></tr>
<tr>
<td width="50%">
<textarea id="input" name="input" style="width:100%;height:400px"><?php echo $data[0];?></textarea>
</td>
<td>
<textarea id="output" name="output" style="width:100%;height:400px"><?php echo $data[1];?></textarea>
</td>
</tr>
<tr>
<td colspan="2"><input type="submit" value="Submit" /></td></tr>
</table>
<input type="hidden" name="id" value="<?php echo $id;?>" />
<input type="hidden" name="file" value="<?php echo $filename;?>" />
</form>
<?php
	require_once "../oj-footer.php";
?>
